﻿// =====================================================
// Copyright (C) 2016-2018 Fanjia
// All rights reserved
// 机器名称: PC-LIWEI
// 创建时间: 2018/11/5 20:40:24
// 创 建 人: 李伟
// ======================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApplication1.Model
{
    // To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
    //
    //    using PoiModel;
    //
    //    var fzPoiGeoModel = FzPoiGeoModel.FromJson(jsonString);

    namespace PoiModel
    {
        using System;
        using System.Collections.Generic;

        using System.Globalization;
        using Newtonsoft.Json;
        using Newtonsoft.Json.Converters;

        public partial class FzPoiGeoModel
        {
            [JsonProperty("status")]
            [JsonConverter(typeof(ParseStringConverter))]
            public long Status { get; set; }

            [JsonProperty("info")]
            public string Info { get; set; }

            [JsonProperty("infocode")]
            [JsonConverter(typeof(ParseStringConverter))]
            public long Infocode { get; set; }

            [JsonProperty("regeocode")]
            public Regeocode Regeocode { get; set; }
        }

        public partial class Regeocode
        {
            [JsonProperty("formatted_address")]
            public string FormattedAddress { get; set; }

            [JsonIgnore]
            public AddressComponent AddressComponent { get; set; }
        }

        public partial class AddressComponent
        {
            [JsonProperty("country")]
            public string Country { get; set; }

            [JsonProperty("province")]
            public string Province { get; set; }

            [JsonProperty("city")]
            public string City { get; set; }

            [JsonProperty("citycode")]
            public string Citycode { get; set; }

            [JsonProperty("district")]
            public string District { get; set; }

            [JsonProperty("adcode")]
            [JsonConverter(typeof(ParseStringConverter))]
            public long Adcode { get; set; }

            [JsonProperty("township")]
            public string Township { get; set; }

            [JsonProperty("towncode")]
            public string Towncode { get; set; }

            [JsonProperty("neighborhood")]
            public Building Neighborhood { get; set; }

            [JsonProperty("building")]
            public Building Building { get; set; }

            [JsonProperty("streetNumber")]
            public StreetNumber StreetNumber { get; set; }

            [JsonProperty("businessAreas")]
            public BusinessArea[] BusinessAreas { get; set; }
        }

        public partial class Building
        {
            [JsonProperty("name")]
            public object[] Name { get; set; }

            [JsonProperty("type")]
            public object[] Type { get; set; }
        }

        public partial class BusinessArea
        {
            [JsonProperty("location")]
            public string Location { get; set; }

            [JsonProperty("name")]
            public string Name { get; set; }

            [JsonProperty("id")]
            [JsonConverter(typeof(ParseStringConverter))]
            public long Id { get; set; }
        }

        public partial class StreetNumber
        {
            [JsonProperty("street")]
            public string Street { get; set; }

            [JsonProperty("number")]
            public string Number { get; set; }

            [JsonProperty("location")]
            public string Location { get; set; }

            [JsonProperty("direction")]
            public string Direction { get; set; }

            [JsonProperty("distance")]
            public string Distance { get; set; }
        }

        public partial class FzPoiGeoModel
        {
            public static FzPoiGeoModel FromJson(string json) => JsonConvert.DeserializeObject<FzPoiGeoModel>(json, PoiModel.Converter.Settings);
        }

        public static class Serialize
        {
            public static string ToJson(this FzPoiGeoModel self) => JsonConvert.SerializeObject(self, PoiModel.Converter.Settings);
        }

        internal static class Converter
        {
            public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
            {
                MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
                DateParseHandling = DateParseHandling.None,
                Converters = {
                new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
            },
            };
        }

        internal class ParseStringConverter : JsonConverter
        {
            public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);

            public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
            {
                if (reader.TokenType == JsonToken.Null) return null;
                var value = serializer.Deserialize<string>(reader);
                long l;
                if (Int64.TryParse(value, out l))
                {
                    return l;
                }
                throw new Exception("Cannot unmarshal type long");
            }

            public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
            {
                if (untypedValue == null)
                {
                    serializer.Serialize(writer, null);
                    return;
                }
                var value = (long)untypedValue;
                serializer.Serialize(writer, value.ToString());
                return;
            }

            public static readonly ParseStringConverter Singleton = new ParseStringConverter();
        }
    }



}
